#pragma once

#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<algorithm>
class Command {
    private:
        void white_list()
        {
            commands.push_back("ls");
            commands.push_back("ll");
            commands.push_back("ls -l");
            commands.push_back("pwd");
            commands.push_back("top");
            commands.push_back("head");
            commands.push_back("history");
        }
    public:
        Command() {
            white_list();
        }
        std::string execute(std::string command) {
            auto it=std::find(commands.begin(), commands.end(), command);
            if(it==commands.end())
            {
                return command+"不支持";
            }
            FILE* fp=popen(command.c_str(), "r");
            if(fp==nullptr)
            {
                return "Fail!";
            }
            char buffer[1024];
            std::string result;
            while(true)
            {
                char *ret=::fgets(buffer,1024,fp);
                if(ret==nullptr)
                    break;
                result+=ret;
            
            }
            pclose(fp);
            return result.empty()?"Done":result;
        }
    private:
        std::vector<std::string> commands;
};